{
 "metadata": {
  "signature": "sha256:438cabc3a6d4779400ddd5a1546c79578f6695205d2a3ebe84743a121881f5ea"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Watershed\n",
      "=========\n",
      "\n",
      "The watershed algorithm (see\n",
      "[1](http://en.wikipedia.org/wiki/Watershed_(algorithm))) is used to\n",
      "split an image into distinct components.\n",
      "\n",
      "Suppose that we have the following image, composed of three whites disks\n",
      "(pixels of value 1) and a black background (pixels of value 0). We want\n",
      "to obtain a new array where each pixel is labeled with the index of the\n",
      "component to which it belongs, that is a segmentation of the orginal\n",
      "array, as shown in the image below. We will use the watershed algorithm\n",
      "provided by scipy.ndimage, scipy.ndimage.watershed\\_ift."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Create the initial black and white image\n",
      "import numpy as np\n",
      "from scipy import ndimage\n",
      "import matplotlib.pyplot as plt\n",
      "a = np.zeros((512, 512)).astype(np.uint8) #unsigned integer type needed by watershed\n",
      "y, x = np.ogrid[0:512, 0:512]\n",
      "m1 = ((y-200)**2 + (x-100)**2 < 30**2)\n",
      "m2 = ((y-350)**2 + (x-400)**2 < 20**2)\n",
      "m3 = ((y-260)**2 + (x-200)**2 < 20**2)\n",
      "a[m1+m2+m3]=1\n",
      "plt.imshow(a, cmap='gray')# left plot in the image above"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "<matplotlib.image.AxesImage at 0x7ff9c8348690>"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAQIAAAD/CAYAAAAaNNgjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADBlJREFUeJzt3U1sHOUdx/HvJtsSHMfGcYpTyQkKUhElxCa8KJY4lAsv\nkTnESgwlUaRQ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aBJAwCSdQeBM11qUJSQ1UTBEPAf4B/\nAm3AbmATsAvIAfkSfZIypFIQLAWWAHcCa4FHgTHgAHAe6Ad2luiTlCGVguA2YD3wX+AnwAbgWHTf\ncaAX6CnRJylD8hXu/wTYCNwO/AU4CRSi+yaBjmi5UHTbgaRMqXaw8N/AG8B3+HrAcBkwDkwU9TVH\nfZIypFIQ3DBj+Y9Ad/RzFzAKjMzoG4mxPkkJqDTC/2vgFuAd4CzwIeE3BCeAdcCz0eNm9gUl1lWq\nT1Iyyn7Wk/yqzyCQGqfsZz3JMwuPJ7gtSV872OgCJEmSJEmSsmIdTsdWtmViVnCaZyf2EJ4W/V1K\n15mm2luAPwGngT+Q/npbgZeA94CngcWku16Ae4A9ZKPW4lnBq5hnvZXmGsRhgK9nJ3YQzk58PYHt\nVuNj4Bzhi1SqzuUl+hpV+4PAk8A14CjwTIna0lTvrcBThK/tKOHclDTXexPwAOFs27S/F4pnBU8B\nPy9RW031JrFLXGrGYhqlfWblO4T/6FeBU4QzQ9Nc7yfR7f3AK5SuLU31bgbejJbTXmvss4KT2CNY\nSfi/AaR7dmJxnWmcWXk1ul1CmPR3kv6ZoGuAHYRvyjHSW+8W4G3Cw68c4XshrbVCHWYFJxEEE4Qz\nFSHdsxOL65yeWZkjfTMrHyOcz/F7vj0TNG31fkZ44Zq9wPdJb707gG1AE+GHawXh7jekr9Zi07OC\n72Ser20ShwZZmZ04s840zqzsBd4FLpGNeqddBF4lvfU+CvQBPwX+CvwMuKuorjTVCnWYFbw43vpK\nOkE4crkC+AHw2wS2Wa17gV8Q7mq9wTfr/A3pqv0JwlH4HwO/JKy5k/TWOwxsB/4H/ItwjCPNry+E\n1+T8EfAr0l3rC4QDmjcAHwB/Jt31SpIkSZIkSZIkSZKkDPk/oJv1S1wwlc8AAAAASUVORK5CYII=\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7ff9c83f6890>"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The watershed algorithm relies on the *flooding* of different basins, so\n",
      "we need to put markers in the image to initiate the flooding. If one\n",
      "knows approximately where the objects are, and there are only a few\n",
      "objects, it is possible to set the markers by hand"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "markers = np.zeros_like(a).astype(np.int16)\n",
      "markers[0, 0] = 1\n",
      "markers[200, 100] = 2\n",
      "markers[350, 400] = 3\n",
      "markers[260, 200] = 4\n",
      "res1 = ndimage.watershed_ift(a.astype(np.uint8), markers)\n",
      "plt.imshow(res1, cmap='jet') # central plot in the image above"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "<matplotlib.image.AxesImage at 0x7ff9c827a550>"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAQIAAAD/CAYAAAAaNNgjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADOZJREFUeJzt3X2MFPUdx/E35dQDDxQing8nXW1qfOBBq0YSm3BNq7Y9\nk0IVn22xhfpUE02riK0KWFs1adWmKRE0NuGqCfhAbDU5jHo0Wo0PETRi01a9tqeJikDlfEShf8xQ\nF9xzZvbmYef2/UomO/fdmd98c7Cfm53d3y5IkiRJkiRJkiRJkiQFRqY0TguwANgDmAk8ntK4knLw\nhZTGmQv0AyuBDcCslMaVlIO0guBYYE24vhboSmlcSTloSWmcfYDN4foA0P7ZTcZtg40pHU5SfO3A\nGyM+b4u0guBtYEy43gas/+wmG4FrUjpcHnqBzoJ7iKuX8vQK5eq3l/L0CrX7XRi5V1pPDXqAqeH6\nlPBnSSWRVhAsAyYSXCTsALpTGldSDtJ6arANuCpcX5HSmAWrFN1AApWiG0ioUnQDCVSKbiChSl17\npXVGMAxVim4ggUrRDSRUKbqBBCpFN5BQpa69DAJJBoEkg0ASBoEkDAJJGASSMAgkYRBIwiCQhEEg\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/wab+VqtrqyL2inhrsttP6H4Cp4c9TgFVAz061nsijlkJf0Q0k0Fd0Awn1Fd1AAn1FN5BQ\nX117RQXBL4DlwNlAd7hMJLgg2AEsC5fqWnddnUgqTNRTg8tq1K4Kb1dE1CSVRJ6v+a/h06cQkvKz\nmnJdoJMkSZIkSSrMZJyOrXIrxazgRp6dOI3gbdG7UrvPRup9LHAn8DJwB43f7x7AzcBDBC9Dj6Sx\n+wU4ClhMOXqtnhV8AEPsN+5bjIdiLp/OTmwneOPR8hyOG8eTwFsEv6RafY6vUSuq9+OBc4GtwLPA\nlTV6a6R+DwIuIfjdriKYm9LI/e5J8BJbK43/f6F6VvCHwAU1ekvUbx6nxLVmLDaiRp9ZeT/BP/oW\nYB3BzNBG7ve58PY4YCm1e2ukfk8G7gnXG73X1GcF53FGsA/BXwOInJ1YqOo+65hZmbkt4W0rQdJP\nYkgzQXNxIDCb4D9lP43b7ynAfQRPv0YQ/F9o1F4h9VnB+QTB2wQzFSFydmKhqvusY2Zlbk4FrgGW\nMKSZoLl4leCDa24H9qVx+50NnAWMJnhw7UVw+g2N12u1lGYF5/PUoCyzE3fusxFnVnYBDwLvUo5+\nt9sE3EXj9nsSMBP4EfAIcD5wRFVfjdQrZDAreGS6/dX0PMGVy72ALwO/zeGYcR0NXERwqnU3O/Z5\nC43V+xkEV+FPBy4m6LmDxu13AXAO8DHwEsE1jkb+/ULwmZzTCWbdNnKv1xNc0NwN+AvwAI3dryRJ\nkiRJkiRJkiRJKpH/AUZSfxIvPywgAAAAAElFTkSuQmCC\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7ff9c8400910>"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.unique(res1) # pixels are tagged according to the object they belong to"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "array([1, 2, 3, 4], dtype=int16)"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "If you don't know where to put the markers, and you know the minimal\n",
      "size of the objects, you can spread a lot of markers on a grid finer\n",
      "than the objects."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "xm, ym = np.ogrid[0:512:10, 0:512:10]\n",
      "markers = np.zeros_like(a).astype(np.int16)\n",
      "markers[xm, ym]= np.arange(xm.size*ym.size).reshape((xm.size,ym.size))\n",
      "res2 = ndimage.watershed_ift(a.astype(np.uint8), markers)\n",
      "res2[xm, ym] = res2[xm-1, ym-1] # remove the isolate seeds\n",
      "plt.imshow(res2, cmap='jet')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 17,
       "text": [
        "<matplotlib.image.AxesImage at 0x7ff9c81a3a10>"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAQIAAAD/CAYAAAAaNNgjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADT1JREFUeJzt3X1sXXUdx/F3146WrQM20Q4c86JiJt0DKMoSo6sPSGRo\nhjBFEUWz+RjjQ4Jhi7AHFZ8SURNdAhIfVjXCkAWUpDNqa+JDAGVjDgk6bbQqShloy7auG/WPc+bu\nyu3uOe157H2/kpN7+rvn9ztfRu/nnnPu+fWCJEmSJEmSJEmSJElSoDmhcVqAjcCpwGXALxMaV1IG\nZiQ0zlpgANgO7ANWJzSupAwkFQQXAjvD9V3AyoTGlZSBloTGmQ8MhevDQMf4DebC2BMJ7UxSdB3A\nv6DpRNskFQSPA3PC9XZgcPwGTwAbEtpZFnqBrpxriKqX8tQK5aq3l/LUCrXr3RShX1KnBj3AsnB9\nafizpJJIKgi2AgsJLhIuALoTGldSBpI6NRgDrg/Xb09ozFxV8i4ghkreBcRUybuAGCp5FxBTZZL9\nkjoimHYqeRcQQyXvAmKq5F1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OdwKXAALAd2AesBtbUaJNUIvWC4EXA+cDfgfcAFwI7w+d2\nASuB5TXaJJVIS53nHwDeACwCfgrsBobD54aAjnB9uOqxgwn0Vq1XwkVSsvrDJY56QXDUw8A2glOE\noxcM5wCDBNcEjra1h201dcUsLk/9lCeo+ilPrVCuevspT61wrN5KVVtfhH71Tg1ax61/C1gW/rwU\n2AH0jGvribDfwuvPu4AY+vMuIKb+vAuIoT/vAmLqn2S/ekHwaeA24B1Ad7gsJLgguADYGi7Vbd2T\nrEVSTuqdGlxbo+368PH2Om2SSiLLz/x3cuwUQlJ2+ijXJTpJkiRJkqTcLMHp2Cq3UswKLvLsxOUE\nt0WfRO06i1T7KcD3gL3ANyl+vacCXwZ+QvAxdDPFrhfgpcAWylFr9azgs5hivVFvMZ6KtRybndhB\ncOPRbRnsN4rfAI8R/CPVqnNejba8ar8IeDfwNPBbYH2N2opU7/OBjxL82+4gmJtS5HpPI/iIrY3i\n/y5UzwoeAT5Qo7ZY9WZxSFxrxmIRFX1m5V0E/9NHgYcIZoYWud4HwsdXALdQu7Yi1Xs5cEe4XvRa\nE58VnMURwXyCdwOoMzsxZ9V1xp5ZmYHR8LGNIOkXM4WZoBk5G7iG4JdygOLWewVwJ8HpVxPB70JR\na4WEZwVDNkHwOMFMRagzOzFn1XXGnlmZobcAG4CbmcJM0Iz8heAP19wKnEFx670GuAqYRfDiOp3g\n8BuKV2u1RGYFQzanBmWZnTi+ziLOrFwJ3AM8RTnqPepJ4PsUt95LgcuA9wI/A94PnFdVV5FqhRRm\nBTcnW19NDxJcuTwdOAf4agb7jOoC4EMEh1rbOL7Or1Cs2t9GcBX+SuDDBDUvoLj1bgSuBg4DfyC4\nxlHkf18I/ibnCoJZt0Wu9XMEFzRbgV8AP6bY9UqSJEmSJEmSJEmSpBL5H2kcoa09QW8PAAAAAElF\nTkSuQmCC\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7ff9c82b2990>"
       ]
      }
     ],
     "prompt_number": 17
    }
   ],
   "metadata": {}
  }
 ]
}